﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using System;
using System.ComponentModel;

namespace Property.Workers
{
    public abstract class FunctionWorkerDecorator<T> : Worker
    {
        protected FunctionWorkerDecorator(IFunctionWorkerPrototype<T> compositionBasePrototype, Func<T> getValue)
        {
            if (compositionBasePrototype == null) throw new ArgumentNullException("compositionBasePrototype");

            _CompositionBase = compositionBasePrototype.Create(getValue);

            if (_CompositionBase == null)
                throw new NotSupportedException(StringsCallbacks.CANT_OBTAIN_WORKER);

            _CompositionBase.RunWorkerCompleted += Completed;
        }

        //=//

        protected readonly IWorker _CompositionBase;

        public override bool IsBusy { get { return _CompositionBase.IsBusy; } }

        protected abstract void Completed(object sender, RunWorkerCompletedEventArgs e);

        public override void Dispose()
        {
            _CompositionBase.RunWorkerCompleted -= Completed;
            _CompositionBase.Dispose();
        }
    }
}
